<template>
  <div id="home">
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <recommend-view :recommends="recommends"></recommend-view>
    <feature/>
    <tab-control @tabClick="tabClick" class="tab-control" :titles="['流行', '新款', '精选']" />
    <goods-list :goods="showGoods"/>
  </div>
</template>

<script>
// 建议再细分一层,分别存储再导入
import NavBar from 'components/common/navbar/NavBar'
import RecommendView from './childComps/RecommendView'
import Feature from './childComps/Feature'
import TabControl from '../../components/content/tabControl/TabControl'
import GoodsList from '../../components/content/goods/GoodsList'

import { 
  getHomeMultidata,
  getHomeGoods
} from '../../network/home'

export default {
  created() {
    // 生命周期中只进行模糊功能(主要逻辑), 具体逻辑交给 method中的函数执行更好
    this.getHomeMultidata()
    this.getHomeGoods('pop')
    this.getHomeGoods('new')
    this.getHomeGoods('sell')
  },
  methods: {
    // 网络请求相关
    getHomeMultidata() {
      getHomeMultidata().then((res) => {
        this.banners = res.data.banner.list
        this.recommends = res.data.recommend.list
      })
    },
    getHomeGoods (type) {
      const page = this.goods[type].page + 1
      getHomeGoods(type, page).then((res) => {
        // console.log(res.data.list)
        // 数组展开后加入,不展开就变成二维数组了 
        this.goods[type].list.push(...res.data.list)
        this.goods[type].page += 1
      })
    },
    // 事件监听相关
    tabClick(index) {
      // console.log(index)
      switch(index) {
        case 0: this.currentType = 'pop' 
          break 
        case 1: this.currentType = 'new'
          break       
        case 2: this.currentType = 'sell' 
          break;
      }
    }
  },
  computed: {
    // 太长有需要的话就设置成计算属性吧
    // 决定 pop/new/sell 要展示的数据
    showGoods() {
      return this.goods[this.currentType].list
    }
  },
  name: 'home',
  data() {
    return {
      banners: null,
      recommends: null,
      goods: {
        'pop': {page: 0, list: []},
        'new': {page: 0, list: []},
        'sell': {page: 0, list: []}
      },
      currentType: 'pop'
    }
  },
  components: {
    NavBar,
    RecommendView,
    Feature,
    TabControl,
    GoodsList
  }
}
</script>

<style scoped>
/* @import url(); 引入css类 */
#home {
  padding-top: 44px;
}
.home-nav {
  background-color: var(--color-tint);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 10;
}
.tab-control {
  /* ???!!! */
  /* TODO 息停效果, sticky -- 根据是否滑动到该块来决定 position是 absolute/fixed */
  background-color: #fff;
  position: sticky;
  top: 44px;
  /* 注意层级 */
  z-index: 10; 
}
</style>
